﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
using System.Globalization;

namespace RapidSpider.DownloadInitialisers
{
    public static partial class DownloadInitialiser
    {
        #region Members
        private static Regex LixInLinkRegex = new Regex(
            "(http://lix.in/[-a-z0-9]*)",
            RegexOptions.IgnoreCase);
        private static Regex thePHPSESSIDRegex = new Regex(
            "<input type=\"hidden\" name=\"PHPSESSID\" value=\"([^\"]*)\" />",
            RegexOptions.IgnoreCase);
        private static Regex theTinyRegex = new Regex(
            "<input type='hidden' name='tiny' value='([^']*)'>",
            RegexOptions.IgnoreCase);
        #endregion

        #region Methods
        public static ArrayList GenerateFileListFromLixInLink(Uri theURI)
        {
            ArrayList theURIList = new ArrayList();

            string thePage = String.Empty;
            string thePHPSESSID = String.Empty;
            string theTiny = String.Empty;

            HttpWebRequest theRequest = (HttpWebRequest)WebRequest.Create(theURI);
            theRequest.UserAgent = "Web-sniffer/1.0.28";

            using (HttpWebResponse theResponse = (HttpWebResponse)theRequest.GetResponse())
            {
                using (Stream theResponseStream = theResponse.GetResponseStream())
                {
                    using (StreamReader theStreamReader = new StreamReader(theResponseStream))
                    {
                        thePage = 
                            theStreamReader.ReadToEnd();
                        thePHPSESSID = 
                            thePHPSESSIDRegex.Match(thePage).Groups[1].Value.ToString();
                        theTiny = 
                            theTinyRegex.Match(thePage).Groups[1].Value.ToString();
                    }
                }
            }

            StringBuilder thePostData = new StringBuilder();
            thePostData.AppendFormat(
                CultureInfo.CurrentCulture,
                "PHPSESSID={0}",
                thePHPSESSID);
            thePostData.AppendFormat(
                CultureInfo.CurrentCulture,
                "&tiny={0}",
                theTiny);
            thePostData.Append("&submit=submit");

            byte[] theEncodedData = new ASCIIEncoding().GetBytes(thePostData.ToString());

            theRequest = (HttpWebRequest)WebRequest.Create(theURI);
            theRequest.ContentType = "application/x-www-form-urlencoded";
            theRequest.ContentLength = theEncodedData.Length;
            theRequest.Method = "POST";

            using (Stream theRequestStream = theRequest.GetRequestStream())
            {
                theRequestStream.Write(theEncodedData, 0, theEncodedData.Length);
            }

            using (HttpWebResponse theResponse = (HttpWebResponse)theRequest.GetResponse())
            {
                using (Stream theResponseStream = theResponse.GetResponseStream())
                {
                    using (StreamReader theStreamReader = new StreamReader(theResponseStream))
                    {
                        theURIList.AddRange(
                            DownloadInitialiser.GenerateRapidshareComFileList(
                                theStreamReader.ReadToEnd()));
                    }
                }
            }
            return DownloadInitialiser.RemoveDuplicates(theURIList);
        }

        #region Private Helpers
        private static ArrayList GenerateLixInFileList(
            string theSource)
        {
            ArrayList theURIList = new ArrayList();

            foreach (Match thisMatch in 
                DownloadInitialiser.LixInLinkRegex.Matches(theSource))
            {
                string thisUri = thisMatch.Groups[1].Value;

                if (!thisUri.Contains("...") && !thisUri.Contains("…") && !thisUri.Contains(".cgi"))
                {
                    thisUri = thisUri.Replace(".html", String.Empty);

                    theURIList.AddRange(
                        DownloadInitialiser.GenerateFileListFromLixInLink(
                            new Uri(thisUri)));
                }
            }
        
            return theURIList;
        }
        #endregion
        #endregion
    }
}
